home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / QuickDraw / QuickDraw™ FX / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-10  |  11.9 KB  |  527 lines  |  [TEXT/KAHL]

  1. /****************************************************************************/
  2. /*                                                                            */
  3. /*    Application:    QuickDraw™ FX                                            */
  4. /*                                                                            */
  5. /*    Description:                                                            */
  6. /*                                                                            */
  7. /*    File:            main.c                                                    */
  8. /*                                                                            */
  9. /*    Files:            FX.π                                                    */
  10. /*                    FX.π.rsrc                                                */
  11. /*                    FX.h                                                    */
  12. /*                    events.c                                                */
  13. /*                    main.c                                                    */
  14. /*                    menu.c                                                    */
  15. /*                                                                            */
  16. /*    Programmer:        Edgar Lee                                                */
  17. /*    Organization:    Apple Computer, Inc. ©1992                                */
  18. /*    Department:        Developer Technical Support, DTS                        */
  19. /*    Language:        C (Think C version 5.0.2)                                */
  20. /*    Date Created:    5-26-92                                                    */
  21. /*                                                                            */
  22. /****************************************************************************/
  23.  
  24. #include "FX.h"
  25.  
  26. /* Global Variable Definitions */
  27.  
  28. WindowPtr        gWindow;
  29. GWorldPtr        gGWorld = nil;
  30. int                gCurrentExample = 11;
  31.  
  32. itemType    tItem[numTItems];
  33. itemType    aItem[numAItems];
  34. itemType    cItem[numCItems];
  35. itemType    dItem[numDItems];
  36. itemType    mItem[numMItems];
  37. itemType    bItem[numBItems];
  38. itemType    pItem[numPItems];
  39. itemType    lItem[numLItems];
  40.  
  41. itemSettings    settings;
  42.  
  43. void drawDeepBox();
  44. void eraseRect();
  45.  
  46. main()
  47. {
  48.     MaxApplZone();
  49.  
  50.     initMac();
  51.     
  52.     createOffscreen( 1 );
  53.     createWindow();
  54.  
  55.     resetItems();
  56.     defineItems();
  57.  
  58.     eventLoop();
  59. }
  60.  
  61. void initMac()
  62. {
  63.     InitGraf( &thePort );
  64.     InitFonts();
  65.     InitWindows();
  66.     InitMenus();
  67.     TEInit();
  68.     InitDialogs( nil );
  69.     InitCursor();
  70.     FlushEvents( 0, everyEvent );
  71.     
  72.     setUpMenus();
  73. }
  74.  
  75. void createOffscreen( pictItem )
  76. int        pictItem;
  77. {
  78.     PicHandle    pict;
  79.     Rect        rect;
  80.     CGrafPtr    currentPort;
  81.     GDHandle    currentDevice;
  82.     
  83.     if (gGWorld != nil)
  84.         DisposeGWorld( gGWorld );
  85.     
  86.     pict = (PicHandle)GetResource( 'PICT', pictItem + 127 );
  87.     
  88.     rect = (**pict).picFrame;
  89.     
  90.     GetGWorld( ¤tPort, ¤tDevice );
  91.     NewGWorld( &gGWorld, 32, &rect, nil, nil, 0 );
  92.         
  93.     LockPixels( (*gGWorld).portPixMap );
  94.     SetGWorld( gGWorld, nil );
  95.  
  96.     DrawPicture( pict, &rect );
  97.     
  98.     SetGWorld( currentPort, currentDevice );
  99.     
  100.     ReleaseResource( pict );
  101. }
  102.  
  103. void createWindow()
  104. {
  105.     int        width, height;
  106.     int        left, top;
  107.     Rect    rect;
  108.     
  109.     width = 58 + ((*gGWorld).portRect.right - (*gGWorld).portRect.left) * 2;
  110.     height = 230 + (*gGWorld).portRect.bottom - (*gGWorld).portRect.top;
  111.     
  112.     left = (((screenBits.bounds.right - screenBits.bounds.left) - width) / 2);
  113.     top = (((screenBits.bounds.bottom - screenBits.bounds.top) - height) / 2) + 20;
  114.  
  115.     SetRect( &rect, left, top, left + width, top + height );
  116.     gWindow = NewCWindow( 0L, &rect, "\pQuickDraw Special Effects", true, noGrowDocProc,
  117.                             (WindowPtr)-1L, true, 0 );
  118.                             
  119.     SetPort( gWindow );
  120. }
  121.  
  122. void resetItems()
  123. {
  124.     settings.tItem = 1;
  125.     settings.aItem = 1;
  126.     settings.cItem = 1;
  127.     settings.mItem = 1;
  128.     settings.dItem = 1;
  129.     settings.pItem = 1;
  130.     settings.lItem = 1;
  131. }
  132.  
  133. void defineItems()
  134. {
  135.     int        i;
  136.     int        col, row;
  137.     
  138.     char    tNames[numTItems][15] = {    "srcCopy", "srcOr", "srcXor", "srcBic",
  139.                                         "notSrcCopy", "notSrcOr", "notSrcXor", "notSrcBic"    };
  140.     char    aNames[numAItems][15] = {    "blend", "addPin", "addOver", "subPin",
  141.                                         "adMax", "subMin", "adMin", "transparent"    };
  142.     char    cNames[numCItems][15] = {    "None", "Inverse", "FG Only", "BG Only"    };
  143.     char    dNames[numDItems][15] = {    "Dithering OFF", "Dithering ON"    };
  144.     char    mNames[numMItems][15] = {    "None", "Search Proc", "ColorTable"    };
  145.     char    bNames[numBItems][15] = {    "White", "Black", "White&Black"    };
  146.     char    pNames[numPItems][15] = {    "SeedCFill", "SeedFill"    };
  147.     char    lNames[numLItems][15] = {    "CalcCMask", "CalcMask"    };
  148.     
  149.     settings.bItem = 1;
  150.     
  151.     col = 15;
  152.     row = (*gWindow).portRect.bottom - 157;
  153.     
  154.     for (i = 0; i < numTItems; i++)
  155.     {
  156.         strcpy( tItem[i].label, tNames[i] );
  157.         CtoPstr( tItem[i].label );
  158.         
  159.         SetRect( &tItem[i].rect, col + 40, row + (i * 15),
  160.                     col + 40 + 90, row + ((i + 1) * 15) );
  161.     }
  162.     
  163.     for (i = 0; i < numAItems; i++)
  164.     {
  165.         strcpy( aItem[i].label, aNames[i] );
  166.         CtoPstr( aItem[i].label );
  167.         
  168.         SetRect( &aItem[i].rect, col + 140, row + (i * 15),
  169.                     col + 140 + 90, row + ((i + 1) * 15) );
  170.     }
  171.     
  172.     for (i = 0; i < numCItems; i++)
  173.     {
  174.         strcpy( cItem[i].label, cNames[i] );
  175.         CtoPstr( cItem[i].label );
  176.         
  177.         SetRect( &cItem[i].rect, col + 240, row + (i * 15),
  178.                     col + 240 + 90, row + ((i + 1) * 15) );
  179.     }
  180.     
  181.     for (i = 0; i < numDItems; i++)
  182.     {
  183.         strcpy( dItem[i].label, dNames[i] );
  184.         CtoPstr( dItem[i].label );
  185.         
  186.         SetRect( &dItem[i].rect, col + 240, row + 90 + (i * 15),
  187.                     col + 240 + 90, row + 90 + ((i + 1) * 15) );
  188.     }
  189.         
  190.     for (i = 0; i < numMItems; i++)
  191.     {
  192.         strcpy( mItem[i].label, mNames[i] );
  193.         CtoPstr( mItem[i].label );
  194.         
  195.         SetRect( &mItem[i].rect, col + 340, row + (i * 15),
  196.                     col + 340 + 90, row + ((i + 1) * 15) );
  197.     }
  198.     
  199.     for (i = 0; i < numBItems; i++)
  200.     {
  201.         strcpy( bItem[i].label, bNames[i] );
  202.         CtoPstr( bItem[i].label );
  203.         
  204.         SetRect( &bItem[i].rect, col + 340, row + 75 + (i * 15),
  205.                     col + 340 + 90, row + 75 + ((i + 1) * 15) );
  206.     }
  207.     
  208.     for (i = 0; i < numPItems; i++)
  209.     {
  210.         strcpy( pItem[i].label, pNames[i] );
  211.         CtoPstr( pItem[i].label );
  212.         
  213.         SetRect( &pItem[i].rect, col + 440, row + (i * 15),
  214.                     col + 440 + 90, row + ((i + 1) * 15) );
  215.     }
  216.     
  217.     for (i = 0; i < numLItems; i++)
  218.     {
  219.         strcpy( lItem[i].label, lNames[i] );
  220.         CtoPstr( lItem[i].label );
  221.         
  222.         SetRect( &lItem[i].rect, col + 440, row + 60 + (i * 15),
  223.                     col + 440 + 90, row + 60 + ((i + 1) * 15) );
  224.     }
  225. }
  226.  
  227. void updateWindow()
  228. {
  229.     long    ticks;
  230.     
  231.     drawBackground();
  232.     drawAllItems();
  233.     drawExampleName();
  234.     drawSourceImage();
  235.     
  236.     ticks = drawFXImage();
  237.     drawTime( ticks );
  238. }
  239.  
  240. void drawBackground()
  241. {
  242.     Rect        rect;
  243.     RGBColor    color;
  244.     
  245.     color.red = color.green = color.blue = 8700;
  246.     
  247.     RGBForeColor( &color );
  248.     PaintRect( &(*gWindow).portRect );
  249.     
  250.     TextFont( times );
  251.     TextMode( srcOr );
  252.     TextSize( 24 );
  253.     
  254.     drawName( 85, 22, "\pSource Image" );
  255.     drawName( (*gWindow).portRect.right - 215, 22, "\pNew Image" );
  256.     
  257.     SetRect( &rect, 15, (*gWindow).portRect.bottom - 180, 
  258.                 (*gWindow).portRect.right - 15, (*gWindow).portRect.bottom - 30 );
  259.     drawDeepBox( &rect );
  260.     
  261.     TextSize( 12 );
  262.     
  263.     drawName( tItem[0].rect.left, tItem[0].rect.top - 8, "\pTransfer Mode" );
  264.     drawName( aItem[0].rect.left, aItem[0].rect.top - 8, "\pArithmetic Mode" );
  265.     drawName( cItem[0].rect.left, cItem[0].rect.top - 8, "\pColorization" );
  266.     drawName( dItem[0].rect.left, dItem[0].rect.top - 8, "\pDither" );
  267.     drawName( mItem[0].rect.left, mItem[0].rect.top - 8, "\pColor Mapping" );
  268.     drawName( bItem[0].rect.left, bItem[0].rect.top - 8, "\pDestination" );
  269.     drawName( pItem[0].rect.left, pItem[0].rect.top - 8, "\pPaint Bucket" );
  270.     drawName( lItem[0].rect.left, lItem[0].rect.top - 8, "\pLasso Tool" );
  271. }
  272.  
  273. void drawAllItems()
  274. {
  275.     drawTransferItems();
  276.     drawArithmeticItems();
  277.     drawColorizeItems();
  278.     drawDitherItems();
  279.     drawMappingItems();
  280.     drawDestinationItems();
  281.     drawPaintBucketItems();
  282.     drawLassoToolItems();
  283. }
  284.  
  285. void drawTransferItems()
  286. {
  287.     int            i;
  288.     Boolean        listEnabled;
  289.  
  290.     listEnabled = (gCurrentExample / 10 == transferID || gCurrentExample / 10 == customID);
  291.     
  292.     for (i = 0; i < numTItems; i++)
  293.         drawItem( tItem[i].rect.left, tItem[i].rect.top, &tItem[i].label,
  294.                     listEnabled, settings.tItem == i + 1 );
  295. }
  296.  
  297. void drawArithmeticItems()
  298. {
  299.     int            i;
  300.     Boolean        listEnabled;
  301.     
  302.     listEnabled = (gCurrentExample / 10 == arithmeticID);
  303.     
  304.     for (i = 0; i < numAItems; i++)
  305.         drawItem( aItem[i].rect.left, aItem[i].rect.top, &aItem[i].label,
  306.                     listEnabled, settings.aItem == i + 1 );
  307. }
  308.  
  309. void drawColorizeItems()
  310. {
  311.     int            i;
  312.     Boolean        listEnabled;
  313.     
  314.     listEnabled = (gCurrentExample / 10 == colorizationID || gCurrentExample / 10 == customID);
  315.         
  316.     for (i = 0; i < numCItems; i++)
  317.         drawItem( cItem[i].rect.left, cItem[i].rect.top, &cItem[i].label,
  318.                     listEnabled, settings.cItem == i + 1 );
  319. }
  320.  
  321. void drawDitherItems()
  322. {
  323.     int            i;
  324.     Boolean        listEnabled;
  325.     
  326.     listEnabled = (gCurrentExample / 10 == ditherID || gCurrentExample / 10 == customID);
  327.         
  328.     for (i = 0; i < numDItems; i++)
  329.         drawItem( dItem[i].rect.left, dItem[i].rect.top, &dItem[i].label,
  330.                     listEnabled, settings.dItem == i + 1 );
  331. }
  332.  
  333. void drawMappingItems()
  334. {
  335.     int            i;
  336.     Boolean        listEnabled;
  337.     
  338.     listEnabled = (gCurrentExample / 10 == searchProcID || gCurrentExample / 10 == customID);
  339.     
  340.     for (i = 0; i < numMItems; i++)
  341.         drawItem( mItem[i].rect.left, mItem[i].rect.top, &mItem[i].label,
  342.                     listEnabled, settings.mItem == i + 1 );
  343. }
  344.  
  345. void drawDestinationItems()
  346. {
  347.     int            i;
  348.     
  349.     for (i = 0; i < numBItems; i++)
  350.         drawItem( bItem[i].rect.left, bItem[i].rect.top, &bItem[i].label,
  351.                     true, settings.bItem == i + 1 );
  352. }
  353.  
  354. void drawPaintBucketItems()
  355. {
  356.     int            i;
  357.     Boolean        listEnabled;
  358.     
  359. //    listEnabled = (gCurrentExample / 10 == paintBucketID);
  360.     listEnabled = false;
  361.     
  362.     for (i = 0; i < numPItems; i++)
  363.         drawItem( pItem[i].rect.left, pItem[i].rect.top, &pItem[i].label,
  364.                     listEnabled, settings.pItem == i + 1 );
  365. }
  366.  
  367. void drawLassoToolItems()
  368. {
  369.     int            i;
  370.     Boolean        listEnabled;
  371.     
  372. //    listEnabled = (gCurrentExample / 10 == lassoID);
  373.     listEnabled = false;
  374.     
  375.     for (i = 0; i < numLItems; i++)
  376.         drawItem( lItem[i].rect.left, lItem[i].rect.top, &lItem[i].label,
  377.                     listEnabled, settings.lItem == i + 1 );
  378. }
  379.  
  380. void drawName( left, top, name )
  381. int        left, top;
  382. Str255    name;
  383. {
  384.     RGBColor    color;
  385.     
  386.     ForeColor( blackColor );
  387.     MoveTo( left + 1, top + 1 );
  388.     DrawString( name );
  389.     
  390.     color.red = color.green = 0xffff;
  391.     color.blue = 40000;
  392.     RGBForeColor( &color );
  393.     
  394.     MoveTo( left, top );
  395.     DrawString( name );
  396. }
  397.  
  398. void drawItem( left, top, label, listEnabled, itemEnabled )
  399. int            left, top;
  400. Str255        *label;
  401. Boolean        listEnabled;
  402. Boolean        itemEnabled;
  403. {
  404.     Rect        rect;
  405.     RGBColor    color, markColor;
  406.     
  407.     if (listEnabled)
  408.         color.red = color.green = color.blue = 0xffff;
  409.     else
  410.         color.red = color.green = color.blue = 0xffff / 2;
  411.         
  412.     SetRect( &rect, left, top + 2, left + 12, top + 14 );
  413.     ForeColor( blackColor );
  414.     FrameOval( &rect );
  415.     
  416.     OffsetRect( &rect, -1, -1 );
  417.     RGBForeColor( &color );
  418.     FrameOval( &rect );
  419.                 
  420.     if (listEnabled && itemEnabled)
  421.         markColor.red = markColor.green = markColor.blue = 0xffff;
  422.     else if (itemEnabled)
  423.         markColor.red = markColor.green = markColor.blue = 0xffff / 2;
  424.     else
  425.         markColor.red = markColor.green = markColor.blue = 8700;
  426.     
  427.     InsetRect( &rect, 3, 3 );
  428.     RGBForeColor( &markColor );
  429.     PaintOval( &rect );
  430.     
  431.     ForeColor( blackColor );
  432.     MoveTo( left + 16, top + 11 );
  433.     DrawString( label );
  434.     
  435.     RGBForeColor( &color );
  436.     MoveTo( left + 15, top + 10 );
  437.     DrawString( label );
  438. }
  439.  
  440. void drawDeepBox( rect )
  441. Rect    *rect;
  442. {
  443.     Rect        box;
  444.     RGBColor    color;
  445.     
  446.     box = *rect;
  447.     box.right += 2;
  448.     box.bottom += 2;
  449.     
  450.     ForeColor( blackColor );
  451.     FrameRect( &box );
  452.     
  453.     OffsetRect( &box, -1, -1 );
  454.     ForeColor( whiteColor );
  455.     FrameRect( &box );
  456. }
  457.  
  458. void drawExampleName()
  459. {
  460.     int            left, top;
  461.     Rect        rect;
  462.     Str255        label = "\pCurrent Example:  ";
  463.     Str255        name[9] = {    "\p TRANSFER MODES", "\pARITHMETIC MODES", "\p   DITHERING",
  464.                             "\p  COLORIZATION", "\p  COLOR MAPPING", "\p  PAINT BUCKET",
  465.                             "\p   LASSO TOOL", "\pPIXEL AVERAGING", "\p    CUSTOM"    };
  466.     
  467.     left = 15;
  468.     top = (*gWindow).portRect.bottom - 13;
  469.     
  470.     drawName( left, top, label );
  471.     left += StringWidth( label );
  472.     
  473.     SetRect( &rect, left - 2, top - 12, left + 130, top + 4 );
  474.     eraseRect( &rect );
  475.     
  476.     drawName( left, top, name[(gCurrentExample / 10) - 1] );
  477. }
  478.  
  479. void drawTime( ticks )
  480. long    ticks;
  481. {
  482.     Rect    rect;
  483.     int        left, top;
  484.     float    seconds;
  485.     char    cString[40];
  486.     
  487.     left = (*gWindow).portRect.right - 130;
  488.     top = (*gWindow).portRect.bottom - 13;
  489.     
  490.     SetRect( &rect, left - 2, top - 12, left + 90, top + 4 );
  491.     eraseRect( &rect );
  492.     
  493.     seconds = (float)ticks / 60.0;
  494.     sprintf( &cString[0], "Draw Time: %03.03f secs", seconds );
  495.     
  496.     drawName( left, top, CtoPstr( cString ) );
  497. }
  498.  
  499. void eraseRect( rect )
  500. Rect    *rect;
  501. {
  502.     RGBColor    color;
  503.     
  504.     color.red = color.green = color.blue = 8700;
  505.     
  506.     RGBForeColor( &color );
  507.     PaintRect( rect );
  508. }
  509.  
  510. void drawSourceImage()
  511. {
  512.     Rect    rect;
  513.     Rect    outlineRect;
  514.  
  515.     SetRect( &rect, 20, 37, 20 + (*gGWorld).portRect.right,
  516.             37 + (*gGWorld).portRect.bottom );
  517.  
  518.     outlineRect = rect;
  519.     InsetRect( &outlineRect, -5, -5 );
  520.     drawDeepBox( &outlineRect );
  521.     
  522.     ForeColor( blackColor );
  523.     BackColor( whiteColor );
  524.     
  525.     CopyBits( (BitMap *)(*(*gGWorld).portPixMap), &gWindow->portBits,
  526.                 &(**(*gGWorld).portPixMap).bounds, &rect, srcCopy, nil );
  527. }